/**
 * 1
 *  
 * @Author : roryrywang
 * @DateTime : 24-9-10 下午6:51
 */

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool cmp(vector<int> a, vector<int> b) {
    return a[0] == b[0] ? a[1] < b[1] : a[0] < b[0];
}

vector<vector<int>> solution(vector<vector<int>> intervals) {
    sort(intervals.begin(), intervals.end(), cmp);
    vector<vector<int>> res;
    int l = -2;
    int r = -2;
    for (auto interval: intervals) {
        if (r < interval[0] - 1) {
            if (r != -2) res.push_back({l, r});

            l = interval[0];
            r = interval[1];
        } else {
            r = max(r, interval[1]);
        }
    }
    if (r != -2) res.push_back({l, r});
    return res;
}